package Window;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
//import java.io.File;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.ObjectOutputStream;
//import java.io.OutputStream;
//import java.net.InetAddress;
//import java.net.Socket;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import Window.DetailWidget.MenuButton;

public class AdminRegister extends JPanel {
	private JTextField userID;
	private JPasswordField password;
	private JTextField accounnumber;
	private JPasswordField confirmpassword;
	public AdminRegister()
	{

		JLabel usernameJLabel  = new JLabel("\u8D26\u53F7");
		usernameJLabel.setFont(new Font("华文新魏", Font.PLAIN, 58));
		usernameJLabel.setBounds(438, 209, 180, 100);
		this.add(usernameJLabel);

		JLabel accountnumberJLabel  = new JLabel("\u7528\u6237\u540D");
		accountnumberJLabel.setFont(new Font("华文新魏", Font.PLAIN, 58));
		accountnumberJLabel.setBounds(428, 96, 180, 100);
		this.add(accountnumberJLabel);

		JLabel passwordJLabel  = new JLabel("\u5BC6\u7801");
		passwordJLabel.setFont(new Font("华文新魏", Font.PLAIN, 58));
		passwordJLabel.setBounds(438, 322, 123, 90);
		add(passwordJLabel);

		JLabel repeatpasswordJLabel  = new JLabel("\u786E\u8BA4\u5BC6\u7801");
		repeatpasswordJLabel.setFont(new Font("华文新魏", Font.PLAIN, 58));
		repeatpasswordJLabel.setBounds(438, 425, 251, 90);
		add(repeatpasswordJLabel);

		userID = new JTextField();//用户名
		userID.setFont(new Font("微软雅黑", Font.PLAIN, 36));
		userID.setBounds(714, 117, 349, 63);
		this.add(userID);

		password = new JPasswordField();//密码
		password.setFont(new Font("微软雅黑", Font.PLAIN, 36));
		password.setBounds(714, 338, 349, 63);
		this.add(password);

		MenuButton buttonregister = new MenuButton();
		buttonregister.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/register.png")));
		buttonregister.setBounds(559, 613, 339, 130);
		this.add(buttonregister);

		accounnumber = new JTextField();//账号
		accounnumber.setFont(new Font("微软雅黑", Font.PLAIN, 36));
		accounnumber.setBounds(714, 227, 349, 63);
		add(accounnumber);

		confirmpassword = new JPasswordField();//确认密码输入框
		confirmpassword.setFont(new Font("微软雅黑", Font.PLAIN, 36));
		confirmpassword.setBounds(714, 452, 349, 63);
		add(confirmpassword);

		AdminRegister message = this;//传递给窗口
		buttonregister.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				buttonregister.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/register2.png")));

			}

			@Override
			public void mousePressed(MouseEvent e) {
				buttonregister.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/register.png")));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				buttonregister.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/register.png")));
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				buttonregister.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/register2.png")));
			}

			@Override
			public void mouseClicked(MouseEvent e) {
			}
		});


		//为注册按钮添加监听器
		buttonregister.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String nameString = userID.getText();
				String idString = accounnumber.getText();
				String passwordString = new String(password.getPassword());
				String confirmpasswordsString = new String(confirmpassword.getPassword());

				if ("".equals(nameString.trim())) {
					JOptionPane.showMessageDialog(null, "用户名不能为空！", "用户名", JOptionPane.ERROR_MESSAGE);
				} else if ("".equals(idString.trim())) {
					JOptionPane.showMessageDialog(null, "账号不能为空！", "账号为空", JOptionPane.ERROR_MESSAGE);
				} else if ("".equals(passwordString.trim())) {
					JOptionPane.showMessageDialog(null, "密码不能为空！", "密码为空", JOptionPane.ERROR_MESSAGE);
				} else if ("".equals(confirmpasswordsString.trim())) {
					JOptionPane.showMessageDialog(message, "信息未填写完整！");
				} else if (!passwordString.equals(confirmpasswordsString)) {
					JOptionPane.showMessageDialog(message, "两次密码不匹配，请重新输入！");
					password.setText("");
					confirmpassword.setText("");
				} else {
					JDBConnection jdbConnection = new JDBConnection();
					String sql = "insert into user values(" + nameString + "," + passwordString + ");";
					if (jdbConnection.executeUpdate(sql) == false) {
						JOptionPane.showMessageDialog(message, "用户名已存在！");
						return;
					}
					JOptionPane.showMessageDialog(message, "注册成功！");
					userID.setText("");
					password.setText("");
					accounnumber.setText("");
					confirmpassword.setText("");
				}
			}

		});
		Background();//背景图片和返回主页按钮功能
	}
	public void Background()
	{
		setSize(MainWindow.WIDTH, MainWindow.HEIGHT);
		setLayout(null);

		//设置背景图片
		JLabel bg_image = new JLabel();
		bg_image.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/background.png")));
		bg_image.setSize(MainWindow.WIDTH,MainWindow.HEIGHT);
		new ImageIcon("image/background.png");


		//返回按钮
		MenuButton button_backButton = new MenuButton();
		button_backButton.setBounds(0,MainWindow.HEIGHT-150,100,100);
		button_backButton.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/button_back.png")));
		this.add(button_backButton);


		button_backButton.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				button_backButton.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/button_back_enter.png")));
				MainWindow.cardLayout.show(MainWindow.mainPanel, "主界面");

			}

			@Override
			public void mousePressed(MouseEvent e) {
				button_backButton.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/button_back_pressed.png")));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				button_backButton.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/button_back.png")));

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				button_backButton.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/button_back_enter.png")));

			}

			@Override
			public void mouseClicked(MouseEvent e) {
			}
		});;
		this.add(bg_image);//将背景添加到容器中
	}
}